﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Ecobee.Net
{
	[JsonConverter(typeof(CamelCaseStringEnumConverter))]
	public enum FanMode
	{
		None,
		On,
		Auto
	}

	[JsonConverter(typeof(CamelCaseStringEnumConverter))]
	public enum ClimateType
	{
		None,
		CalendarEvent,
		Program
	}

	public enum OwnerType
	{
		None,
		AdHoc,
		DemandResponse,
		QuickSave,
		SensorAction,
		SwitchOccupancy,
		System,
		Template,
		User
	}

	/// <summary>
	/// The Schedule object defining the program schedule. 
	/// </summary>	
	public class Climate : ApiObject
	{
		#region Private Fields
		private string _name = string.Empty;
		private string _climateRef = string.Empty;
		private bool _isOccupied = false;
		private bool _isOptimized = false;
		private FanMode _coolFan = FanMode.None;
		private FanMode _heatFan = FanMode.None;
		private VentilatorSetMode _vent = VentilatorSetMode.None;
		private int _ventilatorMinOnTime = 0;
		private OwnerType _owner = OwnerType.None;
		private ClimateType _type = ClimateType.None;
		private ApiColor _color = ApiColor.Empty;
		private ApiTemperature _coolTemp = ApiTemperature.Empty;
		private ApiTemperature _heatTemp = ApiTemperature.Empty;
		#endregion

		[JsonIgnore]
		public override string UniqueKey
		{
			get
			{
				return this.Name;
			}
		}

		/// <summary>
		/// The unique climate name. The name may be changed without affecting the program integrity. 
		/// </summary>
		[JsonProperty(PropertyName = "name")]
		public string Name
		{
			get
			{
				return _name;
			}
			set
			{
				_name= value;
			}
		}

		/// <summary>
		/// The unique climate identifier. Changing the identifier may result in an invalid program as 
		/// Periods which referenced the previous value are left without a climate. This will result 
		/// in a validation error. 
		/// </summary>
		[JsonProperty(PropertyName = "climateRef")]
		public string ClimateReference
		{
			get
			{
				return _climateRef;
			}
			set
			{
				_climateRef= value;
			}
		}

		/// <summary>
		/// A flag indicating whether the property is occupied by persons during this climate.
		/// </summary>
		[JsonProperty(PropertyName = "isOccupied")]
		public bool IsOccupied
		{
			get
			{
				return _isOccupied;
			}
			set
			{
				_isOccupied= value;
			}
		}

		/// <summary>
		/// A flag indicating whether ecobee optimized climate settings are used by this climate.
		/// </summary>
		[JsonProperty(PropertyName = "isOptimized")]
		public bool IsOptimized
		{
			get
			{
				return _isOptimized;
			}
			set
			{
				_isOptimized= value;
			}
		}

		/// <summary>
		/// The cooling fan mode. Default: on. Values: auto, on.
		/// </summary>
		[JsonProperty(PropertyName = "coolFan")]
		public FanMode CoolFan
		{
			get
			{
				return _coolFan;
			}
			set
			{
				_coolFan= value;
			}
		}

		/// <summary>
		/// The heating fan mode. Default: on. Values: auto, on.
		/// </summary>
		[JsonProperty(PropertyName = "heatFan")]
		public FanMode HeatFan
		{
			get
			{
				return _heatFan;
			}
			set
			{
				_heatFan= value;
			}
		}

		/// <summary>
		/// The ventilator mode. Default: off. Values: auto, minontime, on, off.
		/// </summary>
		[JsonProperty(PropertyName = "vent")]
		public VentilatorSetMode Vent
		{
			get
			{
				return _vent;
			}
			set
			{
				_vent= value;
			}
		}

		/// <summary>
		/// The minimum time, in minutes, to run the ventilator each hour.
		/// </summary>
		[JsonProperty(PropertyName = "ventilatorMinOnTime")]
		public int VentilatorMinOnTime
		{
			get
			{
				return _ventilatorMinOnTime;
			}
			set
			{
				_ventilatorMinOnTime= value;
			}
		}

		/// <summary>
		/// The climate owner. Default: system. Values: adHoc, demandResponse, quickSave, sensorAction, switchOccupancy, system, template, user. 
		/// </summary>
		[JsonProperty(PropertyName = "owner")]
		public OwnerType Owner
		{
			get
			{
				return _owner;
			}
			set
			{
				_owner= value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[JsonProperty(PropertyName = "type")]
		public ClimateType Type
		{
			get
			{
				return _type;
			}
			set
			{
				_type= value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[JsonProperty(PropertyName = "colour")]
		public ApiColor Color
		{
			get
			{
				return _color;
			}
			set
			{
				_color= value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[JsonProperty(PropertyName = "coolTemp")]
		public ApiTemperature CoolTemp
		{
			get
			{
				return _coolTemp;
			}
			set
			{
				_coolTemp= value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[JsonProperty(PropertyName = "heatTemp")]
		public ApiTemperature HeatTemp
		{
			get
			{
				return _heatTemp;
			}
			set
			{
				_heatTemp= value;
			}
		}

		protected override string OnDeserializeKeyValue(dynamic dataObject)
		{
			return ((JValue)dataObject.Name).ToObject<string>();
		}

		public override string ToString()
		{
			return this.Name;
		}
	}
}
